{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1,  2,  3,  4],\n",
       "         [ 5,  6,  7,  8,  9],\n",
       "         [10, 11, 12, 13, 14]],\n",
       "\n",
       "        [[15, 16, 17, 18, 19],\n",
       "         [20, 21, 22, 23, 24],\n",
       "         [25, 26, 27, 28, 29]]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(2 * 3 * 5).reshape(2,3,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  0,  0,  0,  0],\n",
       "         [ 5,  6,  0,  0,  0],\n",
       "         [10, 11, 12,  0,  0]],\n",
       "\n",
       "        [[15,  0,  0,  0,  0],\n",
       "         [20, 21,  0,  0,  0],\n",
       "         [25, 26, 27,  0,  0]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tril()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1,  0,  0,  0],\n",
       "         [ 5,  6,  7,  0,  0],\n",
       "         [10, 11, 12, 13,  0]],\n",
       "\n",
       "        [[15, 16,  0,  0,  0],\n",
       "         [20, 21, 22,  0,  0],\n",
       "         [25, 26, 27, 28,  0]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tril(diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1,  2,  0,  0],\n",
       "         [ 5,  6,  7,  8,  0],\n",
       "         [10, 11, 12, 13, 14]],\n",
       "\n",
       "        [[15, 16, 17,  0,  0],\n",
       "         [20, 21, 22, 23,  0],\n",
       "         [25, 26, 27, 28, 29]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.tril(diagonal=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  0,  0,  0,  0],\n",
       "         [ 5,  6,  0,  0,  0],\n",
       "         [10, 11, 12,  0,  0]],\n",
       "\n",
       "        [[15,  0,  0,  0,  0],\n",
       "         [20, 21,  0,  0,  0],\n",
       "         [25, 26, 27,  0,  0]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(2 * 3 * 5).reshape(2,3,5)\n",
    "def tril(target:torch.Tensor, diagonal = 0):\n",
    "    for j in range(target.shape[-2]):\n",
    "        for k in range(diagonal + j + 1, target.shape[-1]):\n",
    "            target[..., j, k] = 0\n",
    "    return target    \n",
    "tril(a, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  0,  0],\n",
       "         [ 3,  0,  0],\n",
       "         [ 6,  7,  0],\n",
       "         [ 9, 10, 11],\n",
       "         [12, 13, 14]],\n",
       "\n",
       "        [[ 0,  0,  0],\n",
       "         [18,  0,  0],\n",
       "         [21, 22,  0],\n",
       "         [24, 25, 26],\n",
       "         [27, 28, 29]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(2 * 3 * 5).reshape(2,5,3)\n",
    "tril(a, -1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
